import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  HttpException,
  HttpStatus,
  Query,
} from '@nestjs/common';
import { CategoryService } from './category.service';
import {
  CategoryListReqDto,
  CreateCategoryDto,
  UpdateCategoryDto,
} from './dto/category.dto';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('小程序/分类')
@Controller('app/category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  /**
   * 获取所有分类
   */
  @Get()
  findAll(@Body() params: CategoryListReqDto) {
    return this.categoryService.findAll(params);
  }
}

@ApiTags('后台/分类')
@Controller('admin/category')
export class AdminCategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  /**
   * 创建分类
   */
  @Post()
  async createCategory(@Body() data: CreateCategoryDto) {
    const category = await this.categoryService.findOneByName(
      data.categoryName,
    );
    if (category) {
      throw new HttpException('分类已存在', HttpStatus.BAD_REQUEST);
    }
    return this.categoryService.save(data);
  }

  /**
   * 获取所有分类
   */
  @Get()
  findAll(@Query() params: CategoryListReqDto) {
    return this.categoryService.findAll(params);
  }

  /**
   * 更新分类
   */
  @Patch(':categoryId')
  updateCategory(
    @Param('categoryId') categoryId: number,
    @Body() data: UpdateCategoryDto,
  ) {
    return this.categoryService.update(categoryId, data);
  }

  /**
   * 删除分类
   */
  @Delete(':categoryId')
  remove(@Param('categoryId') categoryId: number) {
    return this.categoryService.remove(categoryId);
  }
}
